package com.bgs.engineering.api.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bgs.engineering.api.file.entity.Attachments;
import com.bgs.engineering.api.file.entity.FilesAndModules;
import com.bgs.engineering.api.file.mapper.AttachmentsMapper;
import com.bgs.engineering.api.file.mapper.FilesAndModulesMapper;
import com.bgs.engineering.common.errorCode.EmErrorCode;
import com.bgs.engineering.common.response.CommonResult;
import com.bgs.engineering.util.FileToOssUtil;
import com.bgs.engineering.util.pageUtil.Bath;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王宏
 * @version 1.0.0
 * @title FileController
 * @description <TODO description class purpose>
 * <p></p>
 * @creat 2023/7/25 9:02 Tuesday
 **/
@RestController
@CrossOrigin
@RequestMapping("file")
public class FileController {
  @Resource
  private FilesAndModulesMapper filesAndModulesMapper;
  @Resource
  private AttachmentsMapper attachmentsMapper;

  /**
   * @param bath 接受id集合
   * @return com.bgs.engineering.common.response.CommonResult
   * @Author wh
   * @Description 文件删除方法
   * @Date 2023-7-24 10:00:05
   */
  @PostMapping("/deleteFile")
  @ApiOperation(value = "文件删除方法")
  public CommonResult deleteFile(@RequestBody Bath bath) {
    if (FileToOssUtil.deleteFile(bath.getIds())) {
      return CommonResult.ok("文件删除成功");
    } else {
      return CommonResult.error("文件删除失败");
    }
  }

  /**
   * @param tableId 表id
   * @param model   模块
   * @return com.bgs.engineering.common.response.CommonResult
   * @Author wh
   * @Description 文件查询方法
   * @Date 2023年8月1日08:50:47
   */
  @GetMapping("/getFileList")
  @ApiOperation(value = "文件查询方法")
  public CommonResult getFileList(@RequestParam("tableId") Integer tableId, @RequestParam("model") String model) {
    LambdaQueryWrapper<FilesAndModules> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FilesAndModules::getTableId, tableId);
    queryWrapper.eq(FilesAndModules::getModel, model);
    List<FilesAndModules> list = filesAndModulesMapper.selectList(queryWrapper);
    LambdaQueryWrapper<Attachments> queryWrapper1 = new LambdaQueryWrapper<>();
    List<Integer> a = new ArrayList<>();
    for (FilesAndModules filesAndModules : list) {
      a.add(filesAndModules.getFileId());
    }
    queryWrapper1.in(Attachments::getId, a);
    List<Attachments> list1 = attachmentsMapper.selectList(queryWrapper1);
    return CommonResult.ok(list1);
  }


  /**
   * @param file 附件
   * @return com.bgs.engineering.common.response.CommonResult
   * @Author wh
   * @Description 文件上传方法
   * @Date 2023-7-24 10:00:05
   */
  @PostMapping("/fileUpload")
  @ApiOperation(value = "文件上传方法")
  public CommonResult fileUpload(MultipartFile[] file, String model) {
    // 判断文件是否为空
    if (file.length == 0) {
      return CommonResult.error(EmErrorCode.FILE_UPLOAD_ERROR);
    }
    List<Integer> ids = new ArrayList<>();
    for (MultipartFile multipartFile : file) {
      int t = FileToOssUtil.fileToOss(multipartFile, model);
      if (t > 0) {
        ids.add(t);
      }
    }
    return CommonResult.ok(ids);
  }


  /**
   *  获取关联文件
   * @param tableId 主键id
   * @param model  模块名称
   * @return
   */
  @GetMapping("/getRelationFile")
  @ApiOperation(value = "获取关联文件")
  public CommonResult getRelationFile(@RequestParam Integer tableId, @RequestParam String model) {
    //根据模块和id查询关联文件
    LambdaQueryWrapper<FilesAndModules> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FilesAndModules::getModel, model);
    queryWrapper.eq(FilesAndModules::getTableId, tableId);
    List<FilesAndModules> filesAndModules = filesAndModulesMapper.selectList(queryWrapper);
    //声明文件集合
    List<Attachments> res=new ArrayList<>();
    //根据关联文件id查询文件
    for (FilesAndModules record : filesAndModules) {
      Attachments attachments = attachmentsMapper.selectById(record.getFileId());
      //不为空添加到结果集中
      if (attachments!=null){

        res.add(attachments);
      }

    }
    //返回文件集合
    return CommonResult.ok(res);
  }
}
